import { GameObject } from './KobGameObjects'; //没有default的export 需要用大括号
import { Snake } from './Snake'; 
import { Wall } from './Wall' ;
export class GameMap extends GameObject {
    constructor(ctx, parent, store) {
        super();
        this.ctx = ctx;
        this.parent = parent;
        this.L = 0;
        this.store = store;

        this.rows = 13;
        this.cols = 14;

        this.walls = [];

        this.inner_walls_count = 30;
        this.speed = 5; //蛇每秒走5个格子

        this.snakes = [
            new Snake({id: 0, color: "#4876EC", r: this.rows - 2, c : 1}, this),
            new Snake({id: 1, color: "#F94848", c: this.cols - 2, r : 1}, this),
        ]

    }

    

    add_listening_events() {
        if (this.store.state.record.is_record) {
            let k = 0;
            const [snake0, snake1] = this.snakes;
            const a_steps = this.store.state.record.a_steps;
            const b_steps = this.store.state.record.b_steps;
            const loser = this.store.state.record.record_loser;
            console.log("here");
            console.log(this.store.state.record);
            const interval_id = setInterval(() =>  {
                if (k >= a_steps.length - 1) {
                    if (loser === "all" || loser === "A") {
                        console.log("here1");
                        snake0.state = "dead";
                    } 
                    if (loser == "all" || loser === "B") {
                        console.log("here2");
                        snake1.state = "dead";
                    }
                    console.log("before 结束循环");
                    clearInterval(interval_id); //结束循环

                } else {
                    snake0.set_direction(parseInt(a_steps[k]));
                    snake1.set_direction(parseInt(b_steps[k]));
                }
                k++;
            }, 300)
        } else {
            this.ctx.canvas.focus();
            this.ctx.canvas.addEventListener("keydown", e => {
                let d = -1;
    
    
                if (e.key === 'w') d = 0;
                else if (e.key === 'd') d = 1;
                else if (e.key === 's') d = 2;
                else if (e.key === 'a') d = 3;
    
                if (d >= 0) {
                    this.store.state.pk.socket.send(JSON.stringify({
                        event: "move",
                        direction: d,
                    }))
                }
            });
        }
        
    }

    check_valid(cell) { //检测目标位置是否合法
        for (const wall of this.walls) {
            if (wall.r === cell.r && wall.c === cell.c) {
                return false;
            }
        }

        for (const snake of this.snakes) {
            let k = snake.cells.length;
            if (!snake.check_tail_increasing()) { //如果蛇尾要缩，则不需要判断蛇尾
                k --;
            }
            for (let i = 0; i < k; i++) {
                if (snake.cells[i].c === cell.c && snake.cells[i].r === cell.r) {
                    return false;
                }
            }
        }

        return true;
    }


    start() {    
        this.createWalls();    
        this.add_listening_events();
    }

    next_step() {//让两条蛇进入下一回合
        for (const snake of this.snakes) {
            snake.next_step();
        }
    }

    

    checkReady() { //判断两条蛇是否准备好下一回合了
        for (const s of this.snakes) {
            if (s.status !== "idle") {
                return false;
            }

            if (s.direction === -1) {
                return false;
            }
        }

        return true;

    }

    //中心对称矩形
    createWalls() {
        const g = this.store.state.pk.gameMap;

        

        // const g = []; //判断该坐标有无墙
        // for(let r = 0; r < this.rows; r++) {
        //     g[r] = [];
        //     for (let c = 0; c < this.cols; c++) {
        //         g[r][c] = false;
        //     }
        // }

        // //给四周加上障碍物
        // for (let r = 0; r < this.rows; r ++) {
        //     g[r][0] = g[r][this.cols - 1] = true;
        // }

        // for (let c = 0; c < this.cols; c ++) {
        //     g[0][c] = g[this.rows - 1][c] = true;
        // }

        // //创建随机障碍物
        // for (let i = 0; i < this.inner_walls_count / 2; i++)  {
        //     for (let j = 0; j < 1000; j ++) {
        //         let r = parseInt(Math.random() * this.rows);
        //         let c = parseInt(Math.random() * this.cols);
        //         if (g[r][c] || g[this.rows - r - 1][this.cols - c - 1]) {
        //             continue;
        //         }
        //         if ((r === this.rows - 2 && c === 1) || (c === this.cols - 2 && r === 1)) {
        //             continue;
        //         }
                
        //         g[r][c] = g[this.rows - r - 1][this.cols- c - 1] = true;
        //         break;
        //     }
        // }

        // const copy_g = JSON.parse(JSON.stringify(g));
        // if (!this.isConnect(copy_g, this.rows - 2, 1, 1, this.cols - 2)) {
        //     return false;
        // }
        for (let r = 0; r < this.rows; r ++) {
            for (let c = 0; c < this.cols; c++) {
                if (g[r][c]) {
                    this.walls.push(new Wall(r,c,this));
                }
            }
        }
    }

    update_size() {
        this.L = parseInt(Math.min(this.parent.clientWidth / this.cols, this.parent.clientHeight / this.rows));
        this.ctx.canvas.width = this.L * this.cols;
        this.ctx.canvas.height = this.L * this.rows;
    }
    update() {
        this.update_size();
        if (this.checkReady()) {
            this.next_step();
        }
        this.render();
    }

    render() {
        //this.ctx.fillStyle = 'green';
        //this.ctx.fillRect (0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
        const color_even = "#AAD751", color_odd = "#A2D149"
        for (let r = 0; r < this.rows; r++) {
            for (let c = 0; c < this.cols; c++) {
                if ((r + c) % 2 === 0) {
                    this.ctx.fillStyle = color_even;
                } else {
                    this.ctx.fillStyle = color_odd;
                }
                this.ctx.fillRect(c * this.L, r * this.L, this.L, this.L);
            }
        }
    }

    
}